<?php

namespace app\admin\model\school;

use think\Model;
use think\Config;


class StudentStatusChangeRecord extends Model
{



//    protected $dataLimit = 'auth'; //默认基类中为false，表示不启用，可额外使用auth和personal两个值
//    protected $dataLimitField = 'admin_id'; //数据关联字段,当前控制器对应的模型表中必须存在该字段

    // 表名
    protected $name = 'student_status_change_record';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'student_sex_text',
        'major_level_dict_text',
        'before_status_text',
        'student_id_text',
        'after_status_text'
    ];

    protected static function init()
    {
        parent::init(); // TODO: Change the autogenerated stub

        self::afterInsert(function ($model) {

            (new Student())->where("id", $model['student_id'])->update(['status' => $model['after_status']]);

        });

    }


    public function getStudentSexList()
    {
        return ['0' => __('Student_sex 0'), '1' => __('Student_sex 1')];
    }

    public function getMajorLevelDictList()
    {
        return Config::get("site.major_level");
    }

    public function getBeforeStatusList()
    {
        return ['0' => __('Before_status 0'), '1' => __('Before_status 1'), '2' => __('Before_status 2'), '3' => __('Before_status 3')];
    }

    public function getAfterStatusList()
    {
        return ['0' => __('After_status 0'), '1' => __('After_status 1'), '2' => __('After_status 2'), '3' => __('After_status 3')];
    }


    public function getStudentSexTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['student_sex']) ? $data['student_sex'] : '');
        $list = $this->getStudentSexList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getMajorLevelDictTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['major_level_dict']) ? $data['major_level_dict'] : '');
        $list = $this->getMajorLevelDictList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getBeforeStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['before_status']) ? $data['before_status'] : '');
        $list = $this->getBeforeStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getAfterStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['after_status']) ? $data['after_status'] : '');
        $list = $this->getAfterStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStudentIdTextAttr($value, $data)
    {
        $student = (new Student())->field("student_number")->where("id", $data['student_id'])->find();

        return  $student['student_number'];
    }


}
